package com.lsfb.duishangbaos.Login.ShopEnter;

import android.view.View;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.model.LatLng;
import com.lsfb.duishangbaos.R;
import com.lsfb.duishangbaos.Utils.LittleUtils;
import com.lsfb.duishangbaos.Utils.Permission;
import com.zgscwjm.lsfbbasetemplate.Event.LsfbEvent;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.utils.DensityUtils;
import com.zgscwjm.lsfbbasetemplate.view.titlebar.LsfbTitleBar;


/**
 * Created by Administrator on 2017/5/11 0011.
 */
@ContentView(R.layout.aty_location)
public class LocationActivity extends LsfbActivity {

    @ViewInject(R.id.bmapView)
    private MapView mMapView;

    private BaiduMap mBaiduMap;

    private BitmapDescriptor bitmap;
    public LocationClient mLocationClient = null;
    public MyLocationListenner myListener = new MyLocationListenner();
    private MyLocationConfiguration.LocationMode mCurrentMode;
    private BitmapDescriptor mCurrentMarker;
    boolean isFirstLoc = true; // 是否首次定位

    private double lat;
    private double lng;

    private String mlocation = "";


    @Override
    public void initView() {
        LsfbTitleBar titlebar = LittleUtils.setsimpletitlebar(this, "定位");
        TextView textView = LittleUtils.rightbtn(this, "确定");
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LocationEvent event = new LocationEvent();
                event.setData(mlocation);
                LsfbEvent.getInstantiation().post(event);

                LocationEvent2 event2 = new LocationEvent2();
                event2.setData(mlocation);
                LsfbEvent.getInstantiation().post(event2);

                finish();

            }
        });
        titlebar.setRightView(textView, 0, 0, DensityUtils.dp2px(this, 10), 0);


        Permission permission = new Permission(this);
        permission.GetLocationPermission();
        permission.GetLocationPermission2();

        mBaiduMap = mMapView.getMap();

        //普通地图
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);

        mCurrentMode = MyLocationConfiguration.LocationMode.NORMAL;
        mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(
                mCurrentMode, true, mCurrentMarker));
        // 开启定位图层
        mBaiduMap.setMyLocationEnabled(true);
        // 定位初始化
        mLocationClient = new LocationClient(this);
        mLocationClient.registerLocationListener(myListener);
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true);
        // 打开gps
        option.setCoorType("bd09ll");
        // 设置坐标类型
        option.setScanSpan(1000);
        mLocationClient.setLocOption(option);
        mLocationClient.start();

        initListener();
    }

    @Override
    public void initEvent() {

    }

    @Override
    public void initData() {

    }

    /**
     * 定位SDK监听函数
     */
    public class MyLocationListenner implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            // map view 销毁后不在处理新接收的位置
            if (location == null || mMapView == null) {
                return;
            }

            // 是否首次定位
            if (isFirstLoc) {
                isFirstLoc = false;
                LatLng ll = new LatLng(location.getLatitude(),
                        location.getLongitude());
                MapStatus.Builder builder = new MapStatus.Builder();
                builder.target(ll).zoom(18.0f);
                mBaiduMap.animateMapStatus(MapStatusUpdateFactory
                        .newMapStatus(builder.build()));

                java.text.DecimalFormat df = new java.text.DecimalFormat("#.######");
                /**获取经纬度*/
                lat = location.getLatitude();
                lng = location.getLongitude();
                mlocation = df.format(lat) + "," + df.format(lng);
            }
        }

        public void onReceivePoi(BDLocation poiLocation) {
        }

        @Override
        public void onConnectHotSpotMessage(String arg0, int arg1) {
            // TODO Auto-generated method stub

        }
    }

    private void initListener() {
        mBaiduMap.setOnMapStatusChangeListener(new BaiduMap.OnMapStatusChangeListener() {
            @Override
            public void onMapStatusChangeStart(MapStatus status) {
                // updateMapState(status);
            }

            @Override
            public void onMapStatusChangeFinish(MapStatus status) {
                updateMapState(status);
            }

            @Override
            public void onMapStatusChange(MapStatus status) {
//                 updateMapState(status);
            }
        });
    }

    private void updateMapState(MapStatus status) {
        LatLng mCenterLatLng = status.target;
        java.text.DecimalFormat df = new java.text.DecimalFormat("#.######");
        /**获取经纬度*/
        lat = mCenterLatLng.latitude;
        lng = mCenterLatLng.longitude;
        mlocation = df.format(lat) + "," + df.format(lng);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mMapView.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mMapView.onPause();
    }

}
